package edu.nf.ch08.config;

import edu.nf.ch08.interceptor.LoginInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author wangl
 * @date 2025/5/14
 */
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 静态资源处理
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 装配拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截器
        registry.addInterceptor(new LoginInterceptor())
                //拦截所有请求（包括静态资源）
                .addPathPatterns("/**")
                //哪些请求直接放行不经过拦截器
                .excludePathPatterns("/login.html", "/auth", "/page/js/**", "/page/css/**");

        //可以配置多个拦截器
        //registry.addInterceptor(new XxxInterceptor())
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("http://localhost:5000")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*");
    }
}